package com.nowcoder.community.service;

import com.nowcoder.community.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Author: shuhuang
 * @description:
 * @Date: 2022/11/5 18:00
 * @Version java version 1.8
 */
@Service
public class AlphaService {
    private static final Logger logger = LoggerFactory.getLogger(AlphaService.class);

    @Autowired
    private UserService userService;

    /**
     * 声明式事务
     *
     * @return
     */
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
    public boolean save() {
        User user = new User();
        user.setHeaderUrl("test.com");
        user.setUsername("zhangsan");
        user.setStatus(1);
        user.setPassword("123456");
        user.setActivationCode("empty");
        user.setSalt("rewfsf");
        user.setType(0);
        user.setEmail("test@qq.com");
        user.setCreateTime(new Date());
        //传播级别：该方法上没有事务，则创建新事务
        userService.insertUser(user);
        Integer.valueOf("abc");
        return true;
    }

    /**
     * 允许该方法在多线程的环境下，被异步的调用
     */
    @Async
    public void executor1() {
        logger.debug("executor1");
    }

    /**
     * spring线程池定时任务执行体，当项目在多线程环境下启动时会被自动调用
     */
//    @Scheduled(initialDelay = 10000, fixedRate = 1000)
    public void executor2() {
        logger.debug("executor2");
    }
}
